﻿using CCWin;
using System;
using System.Data;
using System.Windows.Forms;

namespace DC.Views
{
    public partial class Form_PatientInfo : Skin_DevExpress
    {
        public Form_PatientInfo()
        {
            InitializeComponent();
        }

        public string patientID = string.Empty; // 传入的病人ID
        public string UserID = string.Empty; // 传入的医生ID

        Utils.Database DB = Utils.Global.GlobalDB;
        private void Form_PatientInfo_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            LoadPatientInfo();
            LoadPatientHistoryExample();
            LoadPatientHistoryFee();
            LoadPrescrition();
            LoadProcess();
        }
        /// <summary>
        /// 加载加工单列表
        /// </summary>
        private void LoadProcess()
        {
            string sql = @"select Process_ID as 序号,
                               Process_NoteNumber as 加工单号, 
                               Process_Firm as 加工单位,
                               Process_SendTime as 取件日期,
                               Process_GetTime  as 回件日期,
                               Process_SenderName as 送件人 From Process WHERE Patient_ID='" + patientID + "'";
            DataView ProcessList = DB.GetDs(sql).Tables[0].DefaultView;
            skinDataGridView1.DataSource = ProcessList;
            skinDataGridView1.Columns[0].Width = 70;
            skinDataGridView1.Columns[1].Width = 250;
            skinDataGridView1.Columns[2].Width = 150;
            skinDataGridView1.Columns[3].Width = 150;
            skinDataGridView1.Columns[4].Width = 150;
            skinDataGridView1.Columns[5].Width = 150;
        }

        /// <summary>
        /// 读入病人信息
        /// </summary>
        private void LoadPatientInfo()
        {
            DataRow info = DB.GetDs("select * from Patient where Patient_ID='" + patientID + "'").Tables[0].DefaultView[0].Row;
            label1.Text = info["Patient_ID"].ToString();
            skinTextBox1.Text = info["Patient_Name"].ToString();
            skinComboBox1.Text = info["Patient_Sex"].ToString();
            dateTimePicker1.Value = Convert.ToDateTime(info["Patient_Birthday"].ToString());
            skinTextBox4.Text = info["Patient_Location"].ToString();
            skinTextBox2.Text = info["Patient_Identity"].ToString();
            skinTextBox6.Text = info["Patient_Phone"].ToString();
            label2.Text = info["Patient_Debt"].ToString();
            label3.Text = info["Patient_Last"].ToString();
        }
        /// <summary>
        /// 加载病人历史病例
        /// </summary>
        private void LoadPatientHistoryExample()
        {
            string sql = @"select [User].Real_Name,
                            Example_ID,
                            Example_Time,
                            Example_Main,
                            IsFirst
                            from Example,[User] where Patient_ID='" + patientID + "' and Example.User_ID=[User].User_ID";
            DataView EeList = DB.GetDs(sql).Tables[0].DefaultView;
            ExampleList.BeginUpdate();
            ExampleList.Items.Clear();

            for (int i = 0; i < EeList.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = EeList[i][1];                    //病例ID
                item.Text = EeList[i][0].ToString(); //主治医师
                item.SubItems.Add(EeList[i][2].ToString()); //就诊时间
                item.SubItems.Add(EeList[i][3].ToString()); //主诉
                if (EeList[i][4].ToString() == "0")
                {
                    item.SubItems.Add("初诊"); //初复诊
                }
                else
                {
                    item.SubItems.Add("复诊"); //初复诊

                }

                ExampleList.Items.Add(item);
            }
            ExampleList.EndUpdate();
        }
        /// <summary>
        /// 加载病人历史收费信息
        /// </summary>
        private void LoadPatientHistoryFee()
        {
            string sql = @"select Fee_ID as 收费单号,
                           Fee_Time as 收费时间,
                           [User].Real_Name as 收费医生,
                           Fee_Price as 本次费用,
                           Fee_Should as 本次应交,
                           Fee_Real as 本次实交,
                           Fee_Debt as 本次欠费,
                           Fee_Payment as 支付方式,
                           (CASE
	                            WHEN Fee_Status=-1 THEN '正常收费'
	                            WHEN Fee_Status=-2 THEN  '作废记录'
	                            WHEN Fee_Status=-3 THEN '退款记录'
	                            END) AS 状态,
                           Fee_Remark As 备注
                           from Fee,[User] where Fee.Patient_ID='" + patientID + "' and Fee.User_ID=[User].User_ID ";

            if (ucCheckBox1.Checked)
            {
                sql += "and Fee.Fee_Status <> -2 order by Fee_ID DESC";
            }
            else
            {
                sql += "order  by Fee_ID DESC";
            }
            skinDataGridView2.DataSource = DB.GetDs(sql).Tables[0].DefaultView;
            skinDataGridView2.Columns[1].Width = 200;
        }

        private void LoadPrescrition()
        {
            DataRow info = DB.GetDs("select * from Patient where Patient_ID='" + patientID + "'").Tables[0].DefaultView[0].Row;
            label_name.Text = info["Patient_Name"].ToString();
            label_sex.Text = info["Patient_Sex"].ToString();
            label_born.Text = info["Patient_Birthday"].ToString();
            label_loction.Text = info["Patient_Location"].ToString();
            string sql = @"select
                            Prescription_ID as 处方号,
                            Prescription_Content as 处方,
                            (CASE
	                            WHEN Prescription_limit=0 THEN '正常处方'
	                            WHEN Prescription_limit=1 THEN  '作废处方'
	                            END) AS 状态
                           from Prescription where Patient_ID='" + patientID + " 'and User_ID='" + UserID ;
            if (ucCheckBox2.Checked)
            {
                sql += "' and Prescription_limit = 0 order by Prescription_ID DESC";
            }
            else
            {
                sql += "'order by Prescription_ID DESC";
            }
            skinDataGridView3.DataSource = DB.GetDs(sql).Tables[0].DefaultView;
            skinDataGridView3.Columns[1].Width = 500;
        }


        private void skinButton2_Click(object sender, EventArgs e)
        {
            Views.Form_NewExample newExample = new Form_NewExample();
            newExample.patientID = patientID;
            newExample.UserID = UserID;
            newExample.ShowDialog();
            LoadPatientHistoryExample();
            LoadPatientInfo();
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            // 新增收费
            Views.Form_Fee fee = new Form_Fee();
            fee.patientID = patientID;
            fee.UserID = UserID;
            fee.Judge = 0;
            fee.ShowDialog();
            LoadPatientHistoryFee();
            LoadPatientInfo();
        }

        private void skinDataGridView2_DoubleClick(object sender, EventArgs e)
        {
            if (skinDataGridView2.SelectedRows.Count >= 1)
            {
                Views.Form_FeeDetail detail = new Form_FeeDetail();
                detail.FeeID = skinDataGridView2.CurrentRow.Cells[0].Value.ToString();
                detail.ShowDialog();
            }
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            //删除病人
            if (label2.Text != "0")
            {
                MessageBox.Show("该病人有欠款无法删除", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Views.Form_NewExample newExample = new Form_NewExample();
            DialogResult dr = MessageBox.Show("确认删除？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string sql = @"DELETE FROM Patient WHERE Patient_ID= " + patientID;
                DB.Execute(sql);
                DB.Execute("Delete From Fee WHERE Patient_ID=" + patientID);
                Close();
            }
            else
            {
                return;
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            string sql = string.Format(@"update Patient set Patient_Name='{0}',Patient_Sex='{1}',Patient_Birthday='{2}',Patient_Location='{3}',
                                                            Patient_Identity='{4}',Patient_Phone='{5}' where Patient_ID=" + label1.Text,
                                                            skinTextBox1.Text,
                                                            skinComboBox1.Text,
                                                            dateTimePicker1.Value.ToString(),
                                                            skinTextBox4.Text,
                                                            skinTextBox2.Text,
                                                            skinTextBox6.Text);
            DB.Execute(sql);
            MessageBox.Show("保存成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ExampleList_DoubleClick(object sender, EventArgs e)
        {

            if (ExampleList.SelectedItems.Count == 0)
            {
                return;
            }
            Views.Form_ViewExample viewExample = new Form_ViewExample();
            viewExample.ExampleID = ExampleList.SelectedItems[0].Tag.ToString();
            viewExample.ShowDialog();
        }

        private void SkinButton8_Click(object sender, EventArgs e)  //作废收费记录
        {
            if (skinDataGridView2.SelectedRows.Count == 0)
            {
                MessageBox.Show("您未选中任何记录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int Delnum = Convert.ToInt32(DB.GetData("select count(*) FROM Fee WHERE Fee_ID >="
                                + Convert.ToInt32(skinDataGridView2.CurrentRow.Cells[0].Value)
                                + " AND Patient_ID=" + patientID
                                + " AND (Fee_Status= -1 OR Fee_Status= -3 )"));
            DialogResult R = DialogResult.No;
            if (Delnum == 1)
            {
                R = MessageBox.Show("您确定要作废这条记录吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
            else
            {
                R = MessageBox.Show("您确定要作废这" + Delnum.ToString() + "条记录吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }

            //输入作废账单理由
            Utils.InputBox box = new Utils.InputBox();
            box.Title = "请输入";
            box.MessageText = "输入作废账单理由：";
            box.ShowDialog();
            if (box.ButtonClick != 1)
            {
                return;
            }
            string name = box.Content;
            if (name == string.Empty)
            {
                MessageBox.Show("作废理由不能为空", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            if (R == DialogResult.Yes)
            {
                double debt = Convert.ToSingle(skinDataGridView2.CurrentRow.Cells[4].Value) - Convert.ToSingle(skinDataGridView2.CurrentRow.Cells[3].Value);

                //将从现在的记录到最后的作废
                string sql = @"update Fee set Fee_Status = -2, Fee_Remark ='" + name + "'"
                                + " WHERE Fee_ID >=" + Convert.ToInt32(skinDataGridView2.CurrentRow.Cells[0].Value)
                                + " AND Patient_ID=" + patientID
                                + " AND (Fee_Status = -1 OR Fee_Status = -3) ";
                DB.Execute(sql);
                string sql_backdebt = @"update Patient set Patient_Debt=" + debt + " WHERE Patient_ID=" + patientID;//返回病人的欠款
                DB.Execute(sql_backdebt);
                LoadPatientHistoryFee();
                LoadPatientInfo();
            }

        }



        private void skinDataGridView2_Click(object sender, EventArgs e)
        {

        }

        private void skinButton9_Click(object sender, EventArgs e)
        {
            Views.Form_BackFee backFee = new Form_BackFee
            {
                PatientID = patientID,
                UserID = UserID,
                debt = Convert.ToSingle(label2.Text)
            };
            backFee.ShowDialog();
            LoadPatientInfo();
            LoadPatientHistoryFee();
        }

        private void ucCheckBox1_CheckedChangeEvent(object sender, EventArgs e)
        {
            LoadData();
        }

        private void skinButton5_Click_1(object sender, EventArgs e)
        {
            //新增加工单
            Views.Form_Process process = new Form_Process();
            process.patientID = patientID;
            process.process_Judge = 1;
            process.ShowDialog();
            LoadProcess();
        }

        private void tabPage4_Click(object sender, EventArgs e)
        {

        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            Views.Form_Prescription prescription = new Form_Prescription();
            prescription.patientID = patientID;
            prescription.UserID = UserID;
            prescription.ShowDialog();
            LoadPrescrition();
        }

        private void skinDataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void skinDataGridView3_Click(object sender, EventArgs e)
        {
            if (skinDataGridView3.SelectedRows.Count > 0)
            {
                if (skinDataGridView3.CurrentRow.Cells[2].Value.ToString() == "作废处方")
                {
                    skinButton7.Enabled = false;
                }
                else
                {
                    skinButton7.Enabled = true;
                }

            }
        }

        private void skinDataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void skinButton7_Click(object sender, EventArgs e)
        {
            if (skinDataGridView3.SelectedRows.Count == 0)
            {
                MessageBox.Show("您未选中任何记录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult R = DialogResult.No;
            R = MessageBox.Show("您确定要作废这条记录吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);


            if (R == DialogResult.Yes)
            {
                string sql = @"update Prescription set Prescription_limit=1  WHERE Prescription_ID=" + Convert.ToInt32(skinDataGridView3.CurrentRow.Cells[0].Value);//返回病人的欠款
                DB.Execute(sql);
                LoadPrescrition();
            }
        }

        private void skinDataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (skinDataGridView1.SelectedRows.Count < 1)
            {
                return;
            }
            if (skinDataGridView1.CurrentRow.Cells[0].Value.ToString() == string.Empty)
            {
                return;
            }
            Views.Form_Process Processchange = new Form_Process();
            Processchange.processID = skinDataGridView1.CurrentRow.Cells[0].Value.ToString();
            Processchange.process_Judge = 2;
            Processchange.patientID = patientID;
            Processchange.ShowDialog();
            LoadProcess();
        }

        private void skinButton10_Click(object sender, EventArgs e)
        {
            if (skinDataGridView1.SelectedRows.Count == 0)
            {
                return;
            }

            DialogResult R = MessageBox.Show("是否删除这条项目", "删除加工单", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (R == DialogResult.Yes)
            {
                string sql = @"delete from Process WHERE Process_ID='" + skinDataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                DB.Execute(sql);
                MessageBox.Show("删除成功", "删除加工单", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadProcess();
            }
        }

        private void ucCheckBox2_CheckedChangeEvent(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
